﻿using System;
using System.Windows;
using PhoneAppRssReader.Entity;
using PhoneAppRssReader.Utls;

namespace PhoneAppRssReader
{
    public partial class ManagerRssFeedPage
    {
        public ManagerRssFeedPage()
        {
            InitializeComponent();
            DataContext = App.Source;
            Loaded += (sender, e) =>
                          {
                              if (!App.Source.IsDataLoad)
                                  App.Source.LoadData();

                          };
        }

        private void Home(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void AddRssFeed(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/AddRssPage.xaml", UriKind.Relative));
        }

        private void EditRssFeed(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.
            var index = itemsListBox.SelectedIndex;
            if (index == -1) return;
            NavigationService.Navigate(new Uri("/EditRssPage.xaml?index=" + index, UriKind.Relative));
        }

        private void DeleteRssFeed(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.

            var deleteFeed = itemsListBox.SelectedItem as Feed;

            if (deleteFeed == null) return;
            var anser = MessageBox.Show("Do you want to delele " + deleteFeed.Title + " ?", "",
                                        MessageBoxButton.OKCancel);
            if (anser != MessageBoxResult.OK) return;
            var success = XmlAction.DeleteFeed(deleteFeed.Id);
            if (success)
            {
                MessageBox.Show("Delete success");
                App.Source.Items.Remove(deleteFeed);
            }
            else
            {
                MessageBox.Show("Delte failure");
            }
        }

        private void AddSampleData(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.

            var success = XmlAction.SaveFeed(FeedSource.GetSampleDataRss());
            if (!success) return;
            //Reload LoadData in App._source
            App.Source.IsDataLoad = false;

            MessageBox.Show("Load Sample data success");
            //Reload items rss feeds
            App.Source.Items.Clear();
            App.Source.LoadData();
        }

       
    }
}